package com.ce.framework.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;
import java.util.Date;

import javax.sql.DataSource;

import com.ce.framework.context.FrameworkContext;
import com.ce.framework.exception.AppException;

public class SystemContext {
	/**
	 * 得到数据库连接
	 * @return
	 * @throws ApplicationException
	 */
	public static Connection getConnection()throws AppException{
		Object obj=FrameworkContext.getBean("dataSource");
		if(obj==null){
			throw new AppException("在系统中没有找到dataSource的注册对象。");
		}
		DataSource myDataSource=(DataSource)obj;
	
		try{
			Connection conn=myDataSource.getConnection();
			return conn;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new AppException(ex.getMessage());
		}
	}
	/**
	 * 得到当前时间
	 * @return
	 * @throws ApplicationException
	 */
	public static Date getNowTime()throws AppException{
		Connection conn=null;
		try{
			conn=getConnection();
			String mySql="select sysdate from dual";
			Statement st=conn.createStatement();
			ResultSet rs=st.executeQuery(mySql);
			if(rs.next()){
				Date nowTime=rs.getTimestamp(1);
				return nowTime;
			}
			
			return new Date();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new AppException(ex.getMessage());
		}finally{
			try{
				if(conn!=null)	conn.close();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new AppException(ex.getMessage());
			}
		}
	}
	/**
	 * 得到当前时间
	 * @return
	 * @throws ApplicationException
	 */
	public static Date getNow()throws AppException{
		Connection conn=null;
		try{
			conn= getConnection();
			String mySql="select sysdate from dual";
			Statement st=conn.createStatement();
			ResultSet rs=st.executeQuery(mySql);
			if(rs.next()){
				Date nowTime=rs.getDate(1);
				return nowTime;
			}
			return new Date();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new AppException(ex.getMessage());
		}finally{
			try{
				if(conn!=null)	conn.close();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new AppException(ex.getMessage());
			}
		}
	}
	
	/**
	 * 得到当前时间
	 * @return
	 * @throws ApplicationException
	 */
	public static Time getTime()throws AppException{
		Connection conn=null;
		try{
			conn= getConnection();
			String mySql="select sysdate from dual";
			Statement st=conn.createStatement();
			ResultSet rs=st.executeQuery(mySql);
			if(rs.next()){
				Time nowTime=rs.getTime(1);
				return nowTime;
			}
			return null;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new AppException(ex.getMessage());
		}finally{
			try{
				if(conn!=null)	conn.close();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new AppException(ex.getMessage());
			}
		}
	}
	
	
	/**
	 * 得到当前时间的日期的字符串型式
	 * @return
	 * @throws ApplicationException
	 */
	public static String getStrNow()throws AppException{
		Date nowTime=getNow();
		String strDate=DateUtil.format(nowTime);
		return strDate;
	}
	
	/**
	 * 取得服务器时间
	 * @return
	 * @throws ApplicationException
	 */
	public static String getStrTime()throws AppException{
		Time time = getTime();
		return time.toString();
	}
}
